package com.jie.common.http.entity.response;

import com.jie.common.http.constant.response.HttpResponseStatusConstant;
import com.jie.common.http.entity.HttpEntityBody;
import com.jie.common.http.entity.HttpHeaderLine;
import com.jie.common.http.util.HttpUtil;
import com.jie.common.util.BytesUtil;

/**
 * @author XieZhiJie
 * @date 2022/04/21 20:55
 */
public class HttpResponse {

    /**
     * 响应行
     */
    private final HttpResponseLine httpResponseLine;

    /**
     * 首部行
     */
    private final HttpHeaderLine httpHeaderLine = new HttpHeaderLine();

    /**
     * 实体
     */
    private final HttpEntityBody httpEntityBody = new HttpEntityBody();

    public HttpResponse() {
        this.httpResponseLine = new HttpResponseLine();
    }

    public HttpResponse(HttpResponseStatusConstant.HttpResponseStatus httpResponseStatus) {
        this(httpResponseStatus.statusCode, httpResponseStatus.statusDescription);
    }

    public HttpResponse(String statusCode, String statusDescription) {
        this.httpResponseLine = new HttpResponseLine(statusCode, statusDescription);
    }

    public HttpResponse(String httpVersion, String statusCode, String statusDescription) {
        this.httpResponseLine = new HttpResponseLine(httpVersion, statusCode, statusDescription);
    }

    /**
     * 获取响应码
     * @return 响应码
     */
    public String getStatusCode() {
        return this.httpResponseLine.getStatusCode();
    }

    /**
     * 获取响应状态描述
     * @return 响应状态描述
     */
    public String getStatusDescription() {
        return this.httpResponseLine.getStatusDescription();
    }

    /**
     * 添加一条报文头
     * @param property 参数名
     * @param value    参数值
     * @return         返回是否添加成功
     */
    public boolean addOneHeaderLine(String property, String value) {
        return this.httpHeaderLine.addOneHeaderLine(property, value);
    }

    /**
     * 根据报文头属性名获取值
     * @param property 属性名
     * @return         返回对应的值
     */
    public String getHeaderLineValue(String property) {
        return this.httpHeaderLine.getHeaderLineMap().get(property);
    }

    /**
     * 获取实体对象
     * @return 返回实体对象
     */
    public Object getEntity() {
        return this.httpEntityBody.getData();
    }

    /**
     * 获取实体对象的String
     * @return 返回实体对象的String
     */
    public String getEntityString() {
        return this.httpEntityBody.toString();
    }

    /**
     * 修改数据实体
     * @param obj 数据实体, 要为可序列化对象
     */
    public void setEntity(Object obj) {
        this.httpEntityBody.setData(obj);
    }

    /**
     * 将这个HttpResponse对象转为字节数组
     * @return 返回字节数组
     */
    public byte[] getMessageBytes() {
        return BytesUtil.packetBytes(this.httpResponseLine.getBytes(), this.httpHeaderLine.getBytes(), this.httpEntityBody.getDataBytes());
    }

    /**
     * 将一个字节数组转为一个HttpResponse对象
     * @param bytes 字节数组
     * @return      返回HttpResponse对象
     */
    public static HttpResponse getHttpResponse(byte[] bytes) {
        // 只能返回Object了, 不过实际上对于序列化回来的对象, 内部还是一个原本的对象
        int bytesIndex = 0;
        HttpResponse httpResponse = new HttpResponse();
        bytesIndex = httpResponse.httpResponseLine.addHeaderLine(bytes, bytesIndex);
        bytesIndex += 2;
        HttpUtil.packageHeaderAndEntity(httpResponse.httpHeaderLine, httpResponse.httpEntityBody, bytes, bytesIndex);
        return httpResponse;
    }

    @Override
    public String toString() {
        return this.httpResponseLine.toString() + this.httpHeaderLine + this.httpEntityBody;
    }
}
